package cn.mxz.robots.province;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 四川省
 * 排名:完成
 * 去年排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-23
 */
public class Sichuan extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("15510117120991", "011701609", "510129199702030642", "", "bl");
		Sichuan gansu = new Sichuan(student);
		System.out.println(gansu.login(""));
	}

	public Sichuan(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request initRequest = new Request("http://cx.sceea.cn/html/GKCJ.htm", mSha1);
		initRequest.setCharSet("utf-8");
		curlPost(initRequest);

		Request loginRequest = new Request("http://api.sceea.cn/Handler/CheckQueryKey.ashx?QueryKey="
				+ mStudent.getStudentId() + "&jsoncallback=?", mSha1);
		curlPost(loginRequest);

		Request login1 = new Request("http://api.sceea.cn/Handler/GetSpcjkHandler.ashx", mSha1);
		login1.addHeader("Referer", "http://cx.sceea.cn/html/GKCJResult.htm");
		login1.addParams("jsoncallPP", "jQuery16020352009776979685_1435026308569");
		login1.addParams("ksh", mStudent.getStudentId());
		login1.addParams("sfzh", mStudent.getIdCard());
		login1.addParams("zkzh", mStudent.getStudentPwd());
		login1.addParams("yzm", urlChineseEncode("请点击"));
		login1.addParams("_", "1435026308681");
		String loginHtml1 = curlPost(login1);

		String jsonHtml = regexMatch(loginHtml1, "jQuery.*\\((.*)\\)");

		JSONObject resultObject = new JSONObject();

		if (jsonHtml.contains("查询成功"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(jsonHtml));
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		JSONObject data = new JSONObject();

		JSONArray parseArray = JSONObject.fromObject(parseHtml).getJSONArray("QResults");
		int resSize = parseArray.size();
		JSONObject scoreObject = new JSONObject();// 成绩
		String name = "";
		String sum = "";
		for (int i = 0; i < resSize; i++)
		{
			JSONObject obj = parseArray.getJSONObject(i);
			if (obj.getString("Name").equals("姓名"))
			{
				name = obj.getString("QValue").replace(" ", "");
			}

			if (i > 1)
			{
				scoreObject.put(obj.getString("Name"), obj.getString("QValue"));
				if(obj.getString("Name").equals("总分"))
					sum = obj.getString("QValue");
			}
		}

		data.put("exam_id", mStudent.getStudentId());
		data.put("name", name);
		data.put("ranking", getRank(57, sum, mStudent.getType()));
		data.put("province", "四川省");

		data.put("score", scoreObject.toString());

		return data.toString();
	}

	@Override
	public void initRank()
	{
		// 理 http://www.gxeduw.com/news/2015/123877.html
		// 文 http://www.gxeduw.com/news/2015/123878.html
		Request request = new Request("http://www.gxeduw.com/news/2015/123877.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text().replaceAll(" ", "")
					.replaceAll("分", ""));
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());

			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(57);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(LI_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
	}

	@Override
	public void lastYearRank()
	{
		// 理 http://www.gxeduw.com/news/2014/106975.html | http://www.gxeduw.com/news/2014/106975_2.html | http://www.gxeduw.com/news/2014/106975_3.html
		// 文 http://www.gxeduw.com/news/2014/106976.html | http://www.gxeduw.com/news/2014/106976_2.html | http://www.gxeduw.com/news/2014/106976_3.html
		Request request = new Request("http://www.gxeduw.com/news/2014/106976_3.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			if(trs.get(i).select("td").get(0).text().replaceAll(" ", "").contains("分数"))
				continue;
			
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text().replaceAll(" ", ""));
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());

			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(26);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(WEN_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
	}
}
